package com.huterox.todoscheduler.core.global;

import com.huterox.todoscheduler.common.SerializationUtils;
import com.huterox.todoscheduler.core.execute.proxy.TodoListExBean;

import java.io.Serializable;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 执行失败的清单对象列表
 * 冷知识：CopyOnWriteArrayList 支持直接迭代删除
 * */
public class TodoListFailedList implements Serializable {

    private static volatile List<TodoListExBean> list;

    private TodoListFailedList() {}

    public static List<TodoListExBean> getInstance() {
        if (list == null) {
            synchronized (TodoListFailedList.class) {
                if (list == null) {
                    list = new CopyOnWriteArrayList<>();
                }
            }
        }
        return list;
    }

    public static List<TodoListExBean> addFailedWithSerializable(TodoListExBean todoListExBean) {
        if (list == null) {
            synchronized (TodoListFailedList.class) {
                if (list == null) {
                    list = new CopyOnWriteArrayList<>();
                }
            }
        }
        list.add(todoListExBean);
        //把这个list进行备份,这里面存放了失败的执行清单，所以这个时候要注意恢复
        SerializationUtils.serializeObject(list,"failedCatalogue","catalogue.ser");
        return list;
    }

    public static List<TodoListExBean> addFailed(TodoListExBean todoListExBean) {
        if (list == null) {
            synchronized (TodoListFailedList.class) {
                if (list == null) {
                    list = new CopyOnWriteArrayList<>();
                }
            }
        }
        list.add(todoListExBean);
        return list;
    }

}
